import { loginAndRegister, getLoginSendcode } from "../../../utils/api/module/login"

Page({
    data: {
        // 本页所有的图片
        imgs: {
            bgImg: "https://ythapi.bjyangshu.com/public/images/login-bg-img.png",
        },
        phoneVal: null,
        codeVal:null,
        passwordVal:null,
        codeText:"获取验证码",
        disabled: true ,
        currentTime: 60,
        userAgreementPath:'/pages/loginAndRegister/userAgreement/index',
        autoFocusCode: false,
        requestFlag: false,
    },
    onLoad: function (options) {

    },
    /**
     * 输入手机号双向绑定
     */
    getPhoneVal(e){
        this.setData({phoneVal: e.detail.value, autoFocusCode: false});
    },
    /**
     * 输入验证码双向绑定
     */
    getCodeVal(e){
        this.setData({codeVal:e.detail.value});
    },
    /**
     * 输入新密码双向绑定
     */
    getPasswordVal(e){
        this.setData({passwordVal: e.detail.value, autoFocusCode: false});
    },
    /**
     * 获取验证码
     */
    getCodeApi(){
        let phone = this.data.phoneVal;
        if(!this.data.disabled){
            return false;
        }else if(phone == null){
            wx.showToast({ title: "请输入电话号", icon: "error" });
            return false;
        }else if(!(/^1[345678]\d{9}$/.test(phone))){
            wx.showToast({   title: "电话号码格式错误", icon: "error" });
            return false;
        }else {
            this.setData({ autoFocusCode: true });
            getLoginSendcode({
                type: 0,
                mobile: phone
            }).then(res => {
                if(res.data.code != 1) {
                    wx.showToast({title: res.data.message, icon: 'none'});
                    return false;
                };
                if(res.data.code == 1) {
                    this.data.codeData = res.data.message;
                    this.setData({ disabled: false });
                    this.times();
                };
            });
        }
    },
    // 倒计时方法
    times: function(options) {
        let that = this;
        let currentTime = that.data.currentTime
        this.data.interval = setInterval(() => {
            currentTime--;
            that.setData({ codeText: currentTime + '秒后重试' })
            if (currentTime <= 0) {
                clearInterval(this.data.interval) //清除定时器
                that.setData({  codeText: '重新发送', currentTime: 60, disabled: true })
            }
        }, 1000)
    },
    /**
     * 点击确认注册账号
     */
    confirmRegister(){
        let phoneVal= this.data.phoneVal;
        let codeVal= this.data.codeVal;
        let passwordVal= this.data.passwordVal;
        let num = /^(\d+[a-zA-Z]+|[a-zA-Z]+\d+)([0-9a-zA-Z]*)$/; // 所含字母数
        if(this.data.requestFlag) return false;
        this.data.requestFlag = true;
        if(phoneVal == null){
            wx.showToast({ title: "请输入电话号", icon: "none" });
            this.data.requestFlag = false;
            return false;
        }
        if(!(/^1[345678]\d{9}$/.test(phoneVal))){
            wx.showToast({ title: "电话号码格式错误", icon: "none" });
            this.data.requestFlag = false;
            return false;
        }
        if(passwordVal== null){
            wx.showToast({ title: "密码不能为空", icon: "none" });
            this.data.requestFlag = false;
            return false;
        }
        if(!this.data.codeVal){
            wx.showToast({ title: "验证码不能为空",  icon: "none" });
            this.data.requestFlag = false;
            return false;
        }
        if(Number(this.data.codeData) !== Number(codeVal)) {
            wx.showToast({ title: "验证码错误",  icon: "none" });
            this.data.requestFlag = false;
            return false;
        }
        if(passwordVal.length < 6 || passwordVal.length > 16 ) {
            wx.showToast({ title: "请输入6~16位密码",  icon: "none" });
               this.data.requestFlag = false;
            return false;
        }
        if(passwordVal.indexOf(' ') != -1) {
            wx.showToast({ title: "密码不能包含空格",  icon: "none" });
            this.data.requestFlag = false;
            return false;
        };
        if(!num.test(passwordVal)) {
            wx.showToast({ title: "所输入的密码中必须含有数字和字母",  icon: "none" });
            this.data.requestFlag = false;
            return false;
        }
        /*api获取验证码*/
        this.setData({
            disabled: false,
        });
        /*调用API注册账户*/
        loginAndRegister({
            type: 0,
            mobile : phoneVal,
            password : passwordVal,
            messagecode : codeVal,
        }).then(res => {
            this.data.requestFlag = false;
            if(res.data.code == 0){
                wx.showToast({
                    title: res.data.message,
                    icon: "none"
                });
                this.setData({
                    disabled: true,
                });
            }else if(res.data.code == 1){
                wx.showToast({ title: res.data.message, icon: "none"});
                //设置sessionid
                wx.setStorageSync("session_id", res.data.sessionid);
                //设置为登录状态
                wx.setStorageSync('isLogin', true);
                wx.setStorageSync('userInfo', res.data.data);
                wx.setStorageSync('userInfoData1', res.data);
                wx.setStorageSync('loginTimer', (new Date).getTime());
                wx.reLaunch({
                    url:'/pages/enterprise/index/index',
                })
                this.setData({
                    disabled: true,
                });
            }else {
                wx.showToast({title: res.data.message, icon: 'none'})
                this.setData({ disabled: true, });
            }
        })
    },
    onHide: function() {
        clearInterval(this.data.interval);
    },
    onUnload: function() {
        clearInterval(this.data.interval);
    }
});